package com.wangtianxiang.rabbitmq.processor;

import com.wangtianxiang.rabbitmq.config.DynamicRabbitProperties;
import com.wangtianxiang.rabbitmq.resolve.PropertiesResolve;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;

public class RabbitMqBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor, EnvironmentAware, InitializingBean {

    private static final Logger logger = LoggerFactory.getLogger(RabbitMqBeanDefinitionRegistryPostProcessor.class);

    private String configPrefix;

    private DynamicRabbitProperties dynamicRabbitProperties;

    private BeanDefinitionRegistry registry;

    private RabbitMqBeanRegister rabbitMqBeanRegister;

    private ConfigurableEnvironment configurableEnvironment;

    private BeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator();

    public RabbitMqBeanDefinitionRegistryPostProcessor(String configPrefix) {
        this.configPrefix = configPrefix;
    }

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        this.registry = registry;
        Assert.notNull(dynamicRabbitProperties,"rabbitMqConfigProperties can not be null");
        Assert.notEmpty(dynamicRabbitProperties.getDataSources(),"rabbitMq datasource can't be empty");
        rabbitMqBeanRegister.registerDatasource(registry,beanNameGenerator);

    }

    /**
     * postProcessBeanFactory方法的执行时机为：BeanFactory标准初始化之后，所有的Bean定义已经被加载，但Bean的实例还没被创建
     * @param beanFactory
     * @throws BeansException
     */
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        logger.info("postProcessBeanFactory ==============");

    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.notNull(dynamicRabbitProperties, "'rabbitMqConfigProperties' is't configgured!");
        rabbitMqBeanRegister = new RabbitMqBeanRegister(dynamicRabbitProperties);
    }

    @Override
    public void setEnvironment(Environment environment) {
      /*  configurableEnvironment = (ConfigurableEnvironment)environment;
        final MutablePropertySources propertyResolver = configurableEnvironment.getPropertySources();
        dynamicRabbitProperties = PropertiesResolve.resolve(propertyResolver,configPrefix, DynamicRabbitProperties.class);
        dynamicRabbitProperties.getDataSources().forEach((key, value)->{
            try {
                value.afterPropertiesSet();
            } catch (Exception e) {
                logger.error("datasource[{}] config error :{}",key,e.getMessage());
                throw new RuntimeException(e);
            }
        });*/

    }
}
